package com.easytopit.demo.controller;

import java.io.File;
import java.io.IOException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import com.easytopit.demo.domain.Comment;
import com.easytopit.demo.domain.Movie;
import com.easytopit.demo.domain.enumDomain.Status;
import com.easytopit.demo.service.MovieService;

@Controller
public class MovieController {
	
	@Autowired
	private MovieService movieService;
	
	@RequestMapping(value="/movieList")
	public String movieListPage(ModelMap modelMap){
		modelMap.put("movies", movieService.queryMovieByStatus("正在上映"));
		modelMap.put("futureMovies", movieService.queryMovieByStatus("即将上映"));
		return "admin/movieList";
	}
	
	@RequestMapping(value="/addMovie.do",method = RequestMethod.POST)
	public String addMovie(String name,@RequestParam("cover")MultipartFile cover,
			String profile,Status status,Timestamp onTime,String country,String director,String length,
			HttpServletRequest request,ModelMap modelMap) throws IllegalStateException, IOException{
		if(movieService.checkName(name)!=null){
			modelMap.put("mes", "alert('该影片已存在');");
			return "index/adminIndex";
		}
		int lastIndexOf = cover.getOriginalFilename().lastIndexOf(".");
		String oriFileNameSuffix = lastIndexOf != -1 ? cover.getOriginalFilename().substring(lastIndexOf): "";
		// 后缀集
		List<String> suffixs = Arrays.asList(".png", ".jpg", ".jpeg", ".gif", ".PNG", ".JPG", ".JPEG", ".GIF");
		if (!suffixs.contains(oriFileNameSuffix)) {
			// 回显信息
			modelMap.put("mes", "alert('请上传图片');");
			// 识别
			return "index/adminIndex";
		}
		String destFileName = UUID.randomUUID() + oriFileNameSuffix;
		String destFilePath = request.getServletContext().getRealPath("/") + "/upload/";
		File destFile = new File(destFilePath, destFileName);
		if(!destFile.getParentFile().exists()) destFile.getParentFile().mkdirs();
		cover.transferTo(destFile);
		movieService.addMovie(name, destFileName, profile,status, onTime, country, director, length);
		modelMap.put("mes", "alert('成功录入');");
		return "index/adminIndex";
	}
	
	@RequestMapping(value="/addVersion.do",method = RequestMethod.POST)
	public String addVersion(String name,String version,double price,ModelMap modelMap){
		if(movieService.checkName(name)!=null){
			movieService.addVersion(name, version, price);
			modelMap.put("mes", "alert('成功录入');");
			return "index/adminIndex";
		}
		if(movieService.checkVersion(version)!=null){
			modelMap.put("mes", "alert('该版本已存在');");
			return "index/adminIndex";
		}
		modelMap.put("mes", "alert('该影片不存在');");
		return "index/adminIndex";
	}
	
	@RequestMapping(value="/addType.do",method = RequestMethod.POST)
	public String addType(String name,String type,ModelMap modelMap){
		if(movieService.checkName(name)!=null){
			movieService.addType(name, type);
			modelMap.put("mes", "alert('成功录入');");
			return "index/adminIndex";
		}
		if(movieService.checkType(type)!=null){
			modelMap.put("mes", "alert('该类型已存在');");
			return "index/adminIndex";
		}
		modelMap.put("mes", "alert('该影片不存在');");
		return "index/adminIndex";
	}
	
	@RequestMapping(value="/addActor.do",method = RequestMethod.POST)
	public String addActor(String name,String actor,ModelMap modelMap){
		if(movieService.checkName(name)!=null){
			movieService.addActor(name, actor);
			modelMap.put("mes", "alert('成功录入');");
			return "index/adminIndex";
		}
		if(movieService.checkActor(actor)!=null){
			modelMap.put("mes", "alert('该演员已存在');");
			return "index/adminIndex";
		}
		modelMap.put("mes", "alert('该影片不存在');");
		return "index/adminIndex";
	}
	
	@RequestMapping(value="/addPhoto.do",method = RequestMethod.POST)
	public String addPhoto(String name,@RequestParam("photo")MultipartFile photo,
			HttpServletRequest request,ModelMap modelMap) throws IllegalStateException, IOException{
		if(movieService.checkName(name)!=null){
			int lastIndexOf = photo.getOriginalFilename().lastIndexOf(".");
			String oriFileNameSuffix = lastIndexOf != -1 ? photo.getOriginalFilename().substring(lastIndexOf): "";
			// 后缀集
			List<String> suffixs = Arrays.asList(".png", ".jpg", ".jpeg", ".gif", ".PNG", ".JPG", ".JPEG", ".GIF");
			if (!suffixs.contains(oriFileNameSuffix)) {
				// 回显信息
				modelMap.put("mes", "alert('请上传图片');");
				// 识别
				return "index/adminIndex";
			}
			String destFileName = UUID.randomUUID() + oriFileNameSuffix;
			String destFilePath = request.getServletContext().getRealPath("/") + "/upload/";
			File destFile = new File(destFilePath, destFileName);
			if(!destFile.getParentFile().exists()) destFile.getParentFile().mkdirs();
			photo.transferTo(destFile);
			movieService.addPhoto(name, destFileName);
			modelMap.put("mes", "alert('成功录入');");
			return "index/adminIndex";
		}
		modelMap.put("mes", "alert('该影片不存在');");
		return "index/adminIndex";
	}
	
	@RequestMapping(value="/addCinema.do",method = RequestMethod.POST)
	public String addCinema(String city,String name,ModelMap modelMap){
		if(movieService.checkCinema(city, name)!=null){
			movieService.addCinema(city,name);
			modelMap.put("mes", "alert('成功录入');");
			return "index/adminIndex";
		}
		modelMap.put("mes", "alert('该影院已存在');");
		return "index/adminIndex";
	}
	
	@RequestMapping(value="/addHall.do",method = RequestMethod.POST)
	public String addHall(String cinemaName,String name,ModelMap modelMap){
		if(movieService.checkHall(cinemaName, name)!=null){
			movieService.addHall(cinemaName, name);
			modelMap.put("mes", "alert('成功录入');");
			return "index/adminIndex";
		}
		modelMap.put("mes", "alert('该影厅已存在');");
		return "index/adminIndex";
	}
	
	@RequestMapping(value = "/movies")
	public String moviesPage(ModelMap modelMap){
		List<Movie> movies =  movieService.movieList();
		List<String> types = new LinkedList<>();
		List<String> actors = new LinkedList<>();
		List<Comment> comment = new LinkedList<>();
		List<Double> score = new LinkedList<>();
		for(Movie movie : movies){
			if(movie!=null){
				System.out.println(movie.getName());
				types = movieService.queryTypeByName(movie.getName());
				actors = movieService.queryActorByName(movie.getName());
				comment = movieService.queryCommentByName(movie.getName());
				score.add(movieService.avg(movie.getName()));
			}
		}
		modelMap.put("movies", movies);
		modelMap.put("types", types);
		modelMap.put("actors", actors);
		modelMap.put("countComment", comment.size());
		modelMap.put("score", score);
		return "movie/movies";
	}
	
	@RequestMapping(value = "/queryMovieByName.do",method = RequestMethod.POST)
	public String queryMovieByName(String name,ModelMap modelMap){
		System.out.println(name);
		List<Movie> movies =  movieService.queryMovieByName("%"+name+"%");
		List<String> types = new LinkedList<>();
		List<String> actors = new LinkedList<>();
		List<String> photos = new LinkedList<>();
		List<Comment> comments = new LinkedList<>();
		List<Double> scores = new LinkedList<>();
		for(Movie movie : movies){
			if(movie!=null){
				types = movieService.queryTypeByName(movie.getName());
				actors = movieService.queryActorByName(movie.getName());
				photos = movieService.queryPhotoByName(movie.getName());
				comments = movieService.queryCommentByName(movie.getName());
				photos = movieService.queryPhotoByName(movie.getName());
				if(movieService.queryScoreByName(movie.getName())!=null){
					scores.add(movieService.avg(movie.getName()));
				}
				modelMap.put("name", movie.getName());
				modelMap.put("cover", movie.getCover());
				modelMap.put("length", movie.getLength());
				modelMap.put("country", movie.getCountry());
				modelMap.put("onTime", movie.getOnTime());
				modelMap.put("director", movie.getDirector());
				modelMap.put("profile", movie.getProfile());
			}
		}
		modelMap.put("types", types);
		modelMap.put("actors", actors);
		modelMap.put("photos", photos);
		modelMap.put("comments", comments);
		modelMap.put("countComment", comments.size());
		modelMap.put("countPhoto", photos.size());
		modelMap.put("scores", scores);
		return "movie/movie";
	}
	
	@RequestMapping(value = "/queryMovieByType.do",method = RequestMethod.POST)
	public String queryMovieByType(String name,ModelMap modelMap){
		List<String> names = movieService.queryMovieByType(name);
		List<Movie> movies = new LinkedList<>();
		List<String> types = new LinkedList<>();
		List<String> actors = new LinkedList<>();
		List<String> photos = new LinkedList<>();
		List<Comment> comments = new LinkedList<>();
		DecimalFormat df = new DecimalFormat("#.00");
		double avg = 0;
		for(int i = 0; i < names.size(); i++){
			movies =  movieService.queryMovieByName(names.get(i));
			for(Movie movie : movies){
				if(movie!=null){
					types = movieService.queryTypeByName(movie.getName());
					actors = movieService.queryActorByName(movie.getName());
					photos = movieService.queryPhotoByName(movie.getName());
					comments = movieService.queryCommentByName(movie.getName());
					avg = Double.valueOf(df.format(movieService.avg(movie.getName())));
				}
			}
		}
		modelMap.put("movies", movies);
		modelMap.put("types", types);
		modelMap.put("actors", actors);
		modelMap.put("photos", photos);
		modelMap.put("comments", comments);
		modelMap.put("countComment", comments.size());
		modelMap.put("avg", avg);
		return "movie/movies";
	}
	
	@RequestMapping(value = "/queryMovieByActor.do",method = RequestMethod.POST)
	public String queryMovieByActor(String name,ModelMap modelMap){
		List<String> names = movieService.queryMovieByActor(name);
		List<Movie> movies = new LinkedList<>();
		List<String> types = new LinkedList<>();
		List<String> actors = new LinkedList<>();
		List<String> photos = new LinkedList<>();
		List<Comment> comments = new LinkedList<>();
		DecimalFormat df = new DecimalFormat("#.00");
		double avg = 0;
		for(int i = 0; i < names.size(); i++){
			movies =  movieService.queryMovieByName(names.get(i));
			for(Movie movie : movies){
				if(movie!=null){
					types = movieService.queryTypeByName(movie.getName());
					actors = movieService.queryActorByName(movie.getName());
					photos = movieService.queryPhotoByName(movie.getName());
					comments = movieService.queryCommentByName(movie.getName());
					avg = Double.valueOf(df.format(movieService.avg(movie.getName())));
				}
			}
		}
		modelMap.put("movies", movies);
		modelMap.put("types", types);
		modelMap.put("actors", actors);
		modelMap.put("photos", photos);
		modelMap.put("comments", comments);
		modelMap.put("count", comments.size());
		modelMap.put("avg", avg);
		return "movie/movies";
	}
	
	@RequestMapping(value = "/queryMovieByDirector.do",method = RequestMethod.POST)
	public String queryMovieByDirector(String name,ModelMap modelMap){
		List<String> movie =  movieService.queryMovieByDirector(name);
		List<String> types = new LinkedList<>();
		List<String> actors = new LinkedList<>();
		List<String> photos = new LinkedList<>();
		List<Comment> comments = new LinkedList<>();
		DecimalFormat df = new DecimalFormat("#.00");
		double avg = 0;
		List<Movie> movies = new LinkedList<>();
		for(int i = 0; i < movie.size(); i++){
			movies = movieService.queryMovieByName(movie.get(i));
		}
		for(Movie m : movies){
			if(m!=null){
				types = movieService.queryTypeByName(m.getName());
				actors = movieService.queryActorByName(m.getName());
				photos = movieService.queryPhotoByName(m.getName());
				comments = movieService.queryCommentByName(m.getName());
				avg = Double.valueOf(df.format(movieService.avg(m.getName())));
			}
		}
		modelMap.put("movies", movies);
		modelMap.put("types", types);
		modelMap.put("actors", actors);
		modelMap.put("photos", photos);
		modelMap.put("comments", comments);
		modelMap.put("count", comments.size());
		modelMap.put("avg", avg);
		return "movie/movie";
	}
	
	@RequestMapping(value = "/queryMovieByCountry.do",method = RequestMethod.POST)
	public String queryMovieByCountry(String name,ModelMap modelMap){
		List<String> movie =  movieService.queryMovieByCountry(name);
		List<String> types = new LinkedList<>();
		List<String> actors = new LinkedList<>();
		List<String> photos = new LinkedList<>();
		List<Comment> comments = new LinkedList<>();
		DecimalFormat df = new DecimalFormat("#.00");
		double avg = 0;
		List<Movie> movies = new LinkedList<>();
		for(int i = 0; i < movie.size(); i++){
			movies = movieService.queryMovieByName(movie.get(i));
		}
		for(Movie m : movies){
			if(m!=null){
				types = movieService.queryTypeByName(m.getName());
				actors = movieService.queryActorByName(m.getName());
				photos = movieService.queryPhotoByName(m.getName());
				comments = movieService.queryCommentByName(m.getName());
				avg = Double.valueOf(df.format(movieService.avg(m.getName())));
			}
		}
		modelMap.put("movies", movies);
		modelMap.put("types", types);
		modelMap.put("actors", actors);
		modelMap.put("photos", photos);
		modelMap.put("comments", comments);
		modelMap.put("count", comments.size());
		modelMap.put("avg", avg);
		return "movie/movie";
	}
}